package com.lipy.dubbodb.rateLimiter;

import com.google.common.util.concurrent.RateLimiter;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class GuavaRateLimiterDemo {

    public static void main(String[] args) throws InterruptedException {
        test();
    }

    public static void test() throws InterruptedException {
        RateLimiter rateLimiter = RateLimiter.create(2.0);

        Thread.sleep(9000);

        ExecutorService executorService = Executors.newFixedThreadPool(5);
        long start = System.currentTimeMillis();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                double acquire = rateLimiter.acquire();

                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("acquire = " + acquire);
                System.out.println(Thread.currentThread().getName() + "- "+ (System.currentTimeMillis()-start));
            }
        };
        for (int i = 0; i < 50; i++) {
            executorService.submit(runnable);
        }
    }
}
